package com.srwl.mytx.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.srwl.mytx.annotation.SerializedField;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;


/**
 * A {@linkplain Converter.Factory converter} which uses FastJson for JSON.
 * <p>
 * Because FastJson is so flexible in the types it supports, this converter assumes that it can
 * handle all types. If you are mixing JSON serialization with something else (such as protocol
 * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add
 * this instance} last to allow the other converters a chance to see their types.
 */
public class MyConverterFactory extends Converter.Factory {

    private ParserConfig mParserConfig = ParserConfig.getGlobalInstance();
    private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
    private Feature[] features;

    private SerializeConfig serializeConfig;
    private SerializerFeature[] serializerFeatures;

    /**
     * Create an default instance for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     *
     * @return The instance of FastJsonConverterFactory
     */
    public static MyConverterFactory create() {
        return new MyConverterFactory();
    }

    private MyConverterFactory() {
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        //如果请求都没有加密，那么返回的数据肯定也没有加密，因为服务端对返回数据加密，需要客户方请求中携带的AES 秘钥
//        boolean encode = true;
//        for (Annotation methodAnnotation : annotations) {
//            if (methodAnnotation instanceof SerializedField) {
//                SerializedField serializedField = (SerializedField) methodAnnotation;
//                encode = serializedField.encode();
//                break;
//            }
//        }
//        if (encode) {
        return new MyResponseBodyConverter<>(type, mParserConfig, featureValues, features);
//        } else {
//            return new MyResponseBodyNotDecodeConverter<>(type, mParserConfig, featureValues, features);
//        }
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        boolean encode = true;
        for (Annotation methodAnnotation : methodAnnotations) {
            if (methodAnnotation instanceof SerializedField) {
                SerializedField serializedField = (SerializedField) methodAnnotation;
                encode = serializedField.encode();
                break;
            }
        }
        if (encode) {//body需要处理，例如添加token 等
            return new MyRequestBodyConverter<>();
        } else {//不需要加密
            return new MyRequestBodyNotEncodeConverter<>();
        }

    }


}
